package dm.ast;

import test.envset.GlobalState;

/**
 * Dm的操作运算符
 * 出去AST中的叶子节点
 */
public class DmOperator extends DmASTNode{
    private String operator = null;  //操作符的字符串形式

    private int expNum = 0;  //表达式数目，表示几元操作符，只能为1或者2

    private boolean isPre = false;  // 一元操作符时，表示是否是前缀，true表示前缀，false表示后缀
    private DmDataType returnType = null ; //操作符表达式返回类型 //如果为null，表示根据表达式决定

    public DmOperator() {
        isCompleted = false;
        isLeaf = true;
    }
    //设置二元操作符
    public void setOperator(String op) {
        this.operator = op;
        this.expNum = 2;
        this.isPre = false;
        isCompleted = true;
        isLeaf = true;
    }

    //设置一元操作符
    public void setOperator(String op , boolean isPre) {
        this.operator = op;
        this.expNum = 1;
        this.isPre = isPre;
        isCompleted = true;
        isLeaf = true;
    }


    public int getExpNum() {
        return expNum;
    }

    public boolean getIsPre() {
        return isPre;
    }

    public String getOperate(){
        return operator;
    }

    public DmDataType getOpDataType() {
        return returnType;
    }

    public void setOpDataType(DmDataType type) {
        this.returnType = type;
    }

    @Override
    public void fill(GlobalState globalState) {
        // TODO: 2024/4/11  
    }

    @Override
    public void setWidt() {
        wid_t = 1;
    }

    @Override
    public void setDept() {
        dep_t = 1;
    }

    @Override
    public DmDataType getExpressionType() {
        return null;
    }

    public String toSql(){
        if(isCompleted){
            return " " + operator + " ";

        }else {
            return " " + this.getClass().getSimpleName();
        }
    }
}
